<template>
  <div class="attendance">
    <h1>工资管理</h1>
    <el-tabs type="border-card">
      <el-tab-pane>
        <span slot="label"><i class="el-icon-date"></i>工资发放记录</span>
        <div class="attendance-search">
          <el-input
            v-model="queryInfo.username"
            placeholder="员工姓名"
            clearable
          ></el-input>
          <el-date-picker
            v-model="queryInfo.month"
            type="month"
            placeholder="选择月"
          >
          </el-date-picker>
          <el-button type="primary" @click="search">查询</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="selection" width="45"> </el-table-column>
          <el-table-column
            type="index"
            :index="1 + (queryInfo.page - 1) * queryInfo.size"
            label="编号"
            width="50"
          >
          </el-table-column>
          <el-table-column prop="nickname" label="姓名" width="80"></el-table-column>
          <el-table-column prop="payday" label="工资发放时间" width="180">
          </el-table-column>
          <el-table-column prop="required_attendance_days" label="应出勤天数">
          </el-table-column>
          <el-table-column prop="actual_attendance_days" label="实际出勤天数">
          </el-table-column>
          <el-table-column prop="basic_salary" label="基本工资(元)">
          </el-table-column>
          <el-table-column prop="actual_salary" label="实发工资(元)">
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <!-- 查看更多 -->
              <span
                @click="showMore(scope.row)"
                style="margin-left: 5px; color: #3d9fff; font-size: 16px;cursor: pointer;"
                >查看更多</span
              >
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <div class="pagination">
          <el-pagination
            :current-page="queryInfo.num"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="queryInfo.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="工资规则">
        <el-descriptions direction="vertical" :column="4" border>
          <el-descriptions-item label="病假一次扣除工资"
            >{{ salaryRule.sick_once_rule }}元</el-descriptions-item
          >
          <el-descriptions-item label="事假一次扣除工资"
            >{{ salaryRule.personal_once_rule }}元</el-descriptions-item
          >
          <el-descriptions-item label="一天缺勤扣除工资" :span="8"
            >{{ salaryRule.absenteeism_once_rule }}元</el-descriptions-item
          >
          <el-descriptions-item label="一天迟到扣除工资">
            {{ salaryRule.late_once_rule }}元
          </el-descriptions-item>
          <el-descriptions-item label="一天早退扣除工资"
            >{{ salaryRule.leave_early_once_rule }}元</el-descriptions-item
          >
        </el-descriptions>
        <a-button type="primary" @click="updateDialogVisible = true">
          修改规则
        </a-button>
      </el-tab-pane>
    </el-tabs>
    <el-dialog title="修改工资规则" :visible.sync="updateDialogVisible" center>
      <el-form :model="salaryRuleForm">
        <el-form-item label="病假一次扣除工资" :label-width="formLabelWidth">
          <el-input
            v-model="salaryRuleForm.sick_once_rule"
            autocomplete
          ></el-input>
        </el-form-item>
        <el-form-item label="事假一次扣除工资" :label-width="formLabelWidth">
          <el-input
            v-model="salaryRuleForm.personal_once_rule"
            autocomplete
          ></el-input>
        </el-form-item>

        <el-form-item label="一天缺勤扣除工资" :label-width="formLabelWidth">
          <el-input
            v-model="salaryRuleForm.absenteeism_once_rule"
            autocomplete
          ></el-input>
        </el-form-item>
        <el-form-item label="一天迟到扣除工资" :label-width="formLabelWidth">
          <el-input
            v-model="salaryRuleForm.late_once_rule"
            autocomplete
          ></el-input>
        </el-form-item>
        <el-form-item label="一天早退扣除工资" :label-width="formLabelWidth">
          <el-input
            v-model="salaryRuleForm.leave_early_once_rule"
            autocomplete
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRule()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="工资详情" :visible.sync="showMoreDialogVisible" center>
      <el-descriptions direction="vertical" :column="4" border>
        <el-descriptions-item label="基本工资">{{ moreData.basic_salary?moreData.basic_salary:0 }}</el-descriptions-item>
        <el-descriptions-item label="实发工资">{{ moreData.actual_salary?moreData.actual_salary :0 }}</el-descriptions-item>
        <el-descriptions-item label="调休天数">{{ moreData.days_off?moreData.days_off:0 }}</el-descriptions-item>
        <el-descriptions-item label="病假天数">{{ moreData.sick_leave_days?moreData.sick_leave_days:0 }}</el-descriptions-item>
        <el-descriptions-item label="事假天数">{{ moreData.personal_leave_days?moreData.personal_leave_days:0 }}</el-descriptions-item>
        <el-descriptions-item label="缺勤天数">{{ moreData.absenteeism_days?moreData.absenteeism_days:0 }}</el-descriptions-item>
        <el-descriptions-item label="迟到天数">{{ moreData.late_days?moreData.late_days:0 }}</el-descriptions-item>

        <el-descriptions-item label="早退天数">{{ moreData.leave_early_days?moreData.leave_early_days:0 }}</el-descriptions-item>
        <el-descriptions-item label="病假扣除工资">{{ moreData.sick_deducted_salary?moreData.sick_deducted_salary:0 }}</el-descriptions-item>
        <el-descriptions-item label="缺勤扣除工资">{{ moreData.absenteeism_deducted_salary?moreData.absenteeism_deducted_salary:0 }}</el-descriptions-item>
        <el-descriptions-item label="迟到扣除工资">{{ moreData.late_deducted_salary?moreData.late_deducted_salary:0 }}</el-descriptions-item>
        <el-descriptions-item label="早退扣除工资">{{ moreData.leave_deducted_salary?moreData.leave_deducted_salary:0 }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { getHistoryClock } from "@/api/clock";
import { getSalaryList, salaryRule, editSalaryRule } from "@/api/user";
import dayjs from "dayjs";
export default {
  data() {
    return {
      showMoreDialogVisible: false,
      tableData: [],
      queryInfo: {
        page: 1, //当前页
        size: 10, //每页最大数
        month: dayjs().format("YYYY-MM")
      },
      total: 10, //总记录数
      salaryRule: {
        leave_early_once_rule: 0,
      },
      salaryRuleForm: {},
      updateDialogVisible: false,
      formLabelWidth: "150px",
      moreData:{}
    };
  },
  created() {
    this.getList();
    this.getSalaryRule();
  },
  methods: {
    getList() {
      let params = this.queryInfo;
      if (this.queryInfo.month) {
        params = {
          ...this.queryInfo,
          month: dayjs(this.queryInfo.month).format("YYYY-MM"),
        };
      }
      getSalaryList(params).then((res) => {
        this.total = res.total;
        this.tableData = res.data;
      });
    },
    getSalaryRule() {
      salaryRule().then((res) => {
        this.salaryRule = res;
        this.salaryRuleForm = res;
      });
    },
    search() {
      this.getList();
    },
    editRule() {
      editSalaryRule(this.salaryRuleForm).then((res) => {
        this.updateDialogVisible = false;
        this.salaryRule = {};
        this.getSalaryRule();
      });
    },
    showMore(moreData) {
      this.moreData = moreData;
      this.showMoreDialogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
.attendance {
  margin: 0 10px;
}

h1 {
  font-size: 24px;
  font-weight: 400;
  padding-bottom: 10px;
  color: #565758;
}

.attendance-search {
  .el-input {
    width: 150px;
    margin-right: 20px;
    margin-bottom: 20px;
  }
}

.pagination {
  width: 400px;
  margin: 10px auto;
}
</style>
